package com.datamation.swadeshi.control.data;

import java.util.ArrayList;

import com.datamation.swadeshi.model.FTranDet;
import com.datamation.swadeshi.model.OrdDet;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;



public class FTranDetDS {
	private SQLiteDatabase dB;
	private DatabaseHelper dbHelper;
	Context context;
	private String TAG="swadeshi";
	
	
	public FTranDetDS (Context context)
	{
		this.context = context;
		dbHelper = new DatabaseHelper(context);
	}
	
	public void open() throws SQLException 
	{
		dB = dbHelper.getWritableDatabase();
	}
	
	
	
	@SuppressWarnings("static-access")
	public int createOrUpdateftrandet(ArrayList<FTranDet>  list) 
	{
		
		int serverdbID =0;
		
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}
		Cursor cursor = null;
		
	try{
		
			
			
    for (FTranDet ftrandet : list) 
    {
		
			ContentValues values = new ContentValues();
			
			
			String selectQuery = "SELECT * FROM " + dbHelper.TABLE_FTRANDET 
					+ " WHERE " + dbHelper.FTRANDET_ID + " = '" +ftrandet.getFTRANDET_ID() + "'";	

			cursor = dB.rawQuery(selectQuery, null);
			
			//values.put(dbHelper.FTRANDET_ID, ftrandet.getFTRANDET_ID());
			values.put(dbHelper.FTRANDET_REFNO, ftrandet.getFTRANDET_REFNO());
			values.put(dbHelper.FTRANDET_TXNDATE, ftrandet.getFTRANDET_TXNDATE());
			values.put(dbHelper.FTRANDET_LOCCODE, ftrandet.getFTRANDET_LOCCODE());
			values.put(dbHelper.FTRANDET_TXNTYPE, ftrandet.getFTRANDET_TXNTYPE());
			values.put(dbHelper.FTRANDET_SEQNO, ftrandet.getFTRANDET_SEQNO());
			values.put(dbHelper.FTRANDET_ITEMCODE, ftrandet.getFTRANDET_ITEMCODE());
			values.put(dbHelper.FTRANDET_QTY, ftrandet.getFTRANDET_QTY());
			values.put(dbHelper.FTRANDET_AMT, ftrandet.getFTRANDET_AMT());
			values.put(dbHelper.FTRANDET_CASEQTY, ftrandet.getFTRANDET_CASEQTY());
			values.put(dbHelper.FTRANDET_PICEQTY, ftrandet.getFTRANDET_PICEQTY());
			values.put(dbHelper.REMQTY, ftrandet.getREMQTY());
			
			int cn = cursor.getCount();
			
			if(cn>0)
			{
			serverdbID = dB.update(dbHelper.TABLE_FTRANDET, values ,dbHelper.FTRANDET_ID + " =?",	
						new String[] { String.valueOf(ftrandet.getFTRANDET_ID())});
				
			}else 
			{
								
			serverdbID = (int) dB.insert(dbHelper.TABLE_FTRANDET, null, values);
			}
			
			}
		}catch (Exception e) {
		
			Log.v("Exception", e.toString());
	
		}finally {  
			if (cursor !=null) {
				cursor.close();
			}
			dB.close();
		}
		return serverdbID;
				
	}
	public ArrayList<String> gettrandet(String RefNo)
	{
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
		
		ArrayList<String> Itemname = new ArrayList<String>();

		String selectQuery = "select * from FTranDet where RefNo = '"+RefNo+"'";
		
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext())
		{
		
			
			Itemname.add(cursor.getString(0));

		}
		
		return Itemname;
	}
	
	public ArrayList<FTranDet> getAllloadDetails(String refno) {
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
		
		ArrayList<FTranDet> list = new ArrayList<FTranDet>();
		
		String selectQuery = "select * from "+dbHelper.TABLE_FTRANDET +" WHERE "+dbHelper.FORDDET_REFNO+"='"+refno+"'";
		
		Cursor cursor = dB.rawQuery(selectQuery, null);
		while(cursor.moveToNext()){
			
			FTranDet ftranset=new FTranDet();
		
			ftranset.setFTRANDET_ID(cursor.getString(cursor.getColumnIndex(dbHelper.FTRANDET_ID)));
			ftranset.setFTRANDET_REFNO(cursor.getString(cursor.getColumnIndex(dbHelper.FTRANDET_REFNO)));
			ftranset.setFTRANDET_TXNDATE(cursor.getString(cursor.getColumnIndex(dbHelper.FTRANDET_TXNDATE)));
			ftranset.setFTRANDET_LOCCODE(cursor.getString(cursor.getColumnIndex(dbHelper.FTRANDET_LOCCODE)));
			ftranset.setFTRANDET_TXNTYPE(cursor.getString(cursor.getColumnIndex(dbHelper.FTRANDET_TXNTYPE)));
			ftranset.setFTRANDET_SEQNO(cursor.getString(cursor.getColumnIndex(dbHelper.FTRANDET_SEQNO)));
			ftranset.setFTRANDET_ITEMCODE(cursor.getString(cursor.getColumnIndex(dbHelper.FTRANDET_ITEMCODE)));
			ftranset.setFTRANDET_QTY(cursor.getString(cursor.getColumnIndex(dbHelper.FTRANDET_QTY)));
			ftranset.setFTRANDET_AMT(cursor.getString(cursor.getColumnIndex(dbHelper.FTRANDET_AMT)));
			ftranset.setFTRANDET_CASEQTY(cursor.getString(cursor.getColumnIndex(dbHelper.FTRANDET_CASEQTY)));
			ftranset.setFTRANDET_PICEQTY(cursor.getString(cursor.getColumnIndex(dbHelper.FTRANDET_PICEQTY)));
			ftranset.setREMQTY(cursor.getString(cursor.getColumnIndex(dbHelper.REMQTY)));
							
			list.add(ftranset);
			
		}
		
		return list;
	}
	@SuppressWarnings("static-access")
	public int deleteOrdDetByID(String id){
		
		int count =0;
		
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}
		Cursor cursor = null;
		try{
			
			cursor = dB.rawQuery("SELECT * FROM " + dbHelper.TABLE_FTRANDET +" WHERE "+dbHelper.FTRANDET_ID+"='"+id+"'", null);
			count =cursor.getCount();
			if(count>0){
				int success = dB.delete(dbHelper.TABLE_FTRANDET, dbHelper.FTRANDET_ID+"='"+id+"'", null);
				Log.v("FtranDet Deleted ", success+"");
			}
		}catch (Exception e){

			Log.v(TAG+" Exception", e.toString());
			
		}finally{  
			if (cursor !=null) {
				cursor.close();
			}
			dB.close();
		}
		
		return count;
		
	}
	@SuppressWarnings("static-access")
	public int DeltranDet(String refno) {
			
		int count =0;
			
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}
		Cursor cursor = null;
			
		try{
						
			String selectQuery = "SELECT * FROM " + dbHelper.TABLE_FTRANDET + " WHERE " + dbHelper.FTRANDET_REFNO + " = '" +refno + "'";	
			cursor = dB.rawQuery(selectQuery, null);
			int cn = cursor.getCount();
				
			if(cn>0){
				count = dB.delete(dbHelper.TABLE_FTRANDET, dbHelper.FTRANDET_REFNO + " ='"+refno+"'", null);
				Log.v("Success Stauts", count+"");
			}
		}catch (Exception e) {
			Log.v(TAG+ " Exception", e.toString());
		}
			finally {  
				if (cursor !=null) {
					cursor.close();
					}
				dB.close();
			}
		return count;
					
	}
	
	@SuppressWarnings("static-access")
	public int restData(String refno) {
			
		int count =0;
			
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}
		Cursor cursor = null;
			
		try{
						
			String selectQuery = "SELECT * FROM " + dbHelper.TABLE_FTRANDET + " WHERE " + dbHelper.FTRANDET_REFNO + " = '" +refno + "'";	
			cursor = dB.rawQuery(selectQuery, null);
			int cn = cursor.getCount();
				
			if(cn>0){
				count = dB.delete(dbHelper.TABLE_FTRANDET, dbHelper.FTRANDET_REFNO + " ='"+refno+"'", null);
				Log.v("Success Stauts", count+"");
			}
		}catch (Exception e) {
			Log.v(TAG+ " Exception", e.toString());
		}
			finally {  
				if (cursor !=null) {
					cursor.close();
					}
				dB.close();
			}
		return count;
					
	}
	
	
	public String getTotalReturns(String refno) {
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}

		//String selectQuery = "SELECT * FROM " + dbHelper.TABLE_FREASON +" WHERE "+dbHelper.FREASON_NAME+"='"+name+"'";
		@SuppressWarnings("static-access")
		String selectQuery = "SELECT SUM(Amt) as totamt FROM FTRANDET WHERE refno='"+refno+"'";
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext()){
			if (cursor.getString(cursor.getColumnIndex("totamt"))!=null)
				return cursor.getString(cursor.getColumnIndex("totamt"));
			else
				return "0.00";
			
		}
		
		return "0.00";
	}
	
}
